<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Info extends CI_Controller {

	function __construct(){
		parent::__construct();
		authRedirect('/admin/info');
		$this->load->model('info_model');
	}
	public function index()
	{
		$data['error'] = $this->session->flashdata('error');
		$data['success'] = $this->session->flashdata('success');
		
		$this->load->view('admin/info/list.tpl',$data);
	}

	public function action(){
		
		$data['error'] = $this->session->flashdata('error');
		$data['success'] = $this->session->flashdata('success');
		
		$type = $this->uri->segment(4);
		$types = array('about','brand','contribution','contact','join','friend','japan');
		if( !in_array($type,$types)){show_404();}

		$this->load->library('form_validation');
		$this->form_validation->set_rules('content', '内容', 'trim|required');
		if ($this->form_validation->run()) {
			$r = $this->info_model->action($type);
			if($r['status']){
				$this->session->set_flashdata('success','添加成功！');
				redirect('/admin/info');
			}else{
				$this->session->set_flashdata('error',$r['msg']);
				redirect('/admin/info/action');
			}
		}
		switch($type){
			case 'about':$data['title'] = '关于我们';break;
			case 'brand':$data['title'] = '品牌故事';break;
			case 'contribution':$data['title'] = '社会贡献';break;
			case 'contact':$data['title'] = '联系我们';break;
			case 'join':$data['title'] = '加入我们';break;
			case 'friend':$data['title'] = '友情链接';break;
			case 'japan':$data['title'] = '日本定制';break;
		}
		$data['info'] = '';
		$data['action'] = '/admin/info/action/'.$type;
		$data['button'] = '添加'.$data['title'];
		$info = $data['info'] = $this->info_model->get_info_bytype($type);
		if($info){
			$data['action'] = '/admin/info/update/' . $type;
			$data['button'] = '更新'.$data['title'];
		}

		$this->load->view('admin/info/action.tpl',$data);
	}

	public function update(){
	$type = $this->uri->segment(4);
		$types = array('about','brand','contribution','contact','join','friend','japan');
		if( !in_array($type,$types)){show_404();}

		$info = $data['info'] = $this->info_model->get_info_bytype($type);
		if(!$info)show_404();


		$this->load->library('form_validation');
		$this->form_validation->set_rules('content', '内容', 'trim|required');
		if($this->form_validation->run()){
			$r = $this->info_model->update($type);
			if($r['status']){
				$this->session->set_flashdata('success','更新成功！');
				redirect('/admin/info');
			}else{
				$this->session->set_flashdata('error',$r['msg']);
				redirect('/admin/info/action/' . $type);
			}
		}else{
			$this->session->set_flashdata('error',validation_errors());
			redirect('/admin/news/action/' . $id);
		}
	}
}

/* End of file production.php */
/* Location: ./application/controllers/admin/production.php */